﻿using System;
using System.Collections.Generic;
using System.Linq;

using System.Text;
using System.Threading.Tasks;
using CL_VisionCommu_Test.Helper;
using CLVisionTcpIP;

namespace CL_VisionCommu_Test.Device
{
    class TcpIPClient_Communication : Abstract_Communication
    {
        CommuTcpIPClientCaller caller = new CommuTcpIPClientCaller();
   

        public override string Init(string Path, string mark)
        {
            platForm = mark;
            caller.ncOrTS = this.platForm;
            return caller.Init();

        }

        public override double ReadConstantValue(int Index, string ConstantName)
        {
            try
            {
                return ((double)caller.ReadTSConstantValue(Index, ConstantName));
            }
            catch (Exception ex)
            {
                throw new AggregateException(ex.Message);
            }
        }

        public override double ReadConstantValue(int Index, string ConstantName, bool mark)
        {
            try
            {
                return ((double)caller.ReadNCConstantValue(Index, ConstantName));
            }
            catch (Exception ex)
            {
                throw new AggregateException(ex.Message);
            }
        }

        public override List<string> ReadObjectValue(int Index, ObjectItems objectItems)
        {
            try
            {
                return caller.ReadObject(Index, objectItems);
            }
            catch (Exception ex)
            {
                throw new AggregateException(ex.Message);
            }
        }

        public override bool SendConstantValue(int Index, string ConstantName, double Value)
        {
            try
            {
                return caller.WriteTSConstantValue(Index, ConstantName, Value);
            }
            catch (Exception ex)
            {
                throw new AggregateException(ex.Message);
            }
        }

        public override bool SendConstantValue(int Index, string ConstantName, double Value, bool mark)
        {
            try
            {
                return caller.WriteNCConstantValue(Index, ConstantName, Value);
            }
            catch (Exception ex)
            {
                throw new AggregateException(ex.Message);
            }
        }

        public override bool SendObjectValue(int Index, ObjectItems objectItems)
        {

            try
            {
                return caller.WriteObject(Index, objectItems);
            }
            catch (Exception ex)
            {
                throw new AggregateException(ex.Message);
            }
        }

        public override string GetStatus(int Index)
        {
            try
            {
                if (platForm == "TS")
                {
                    string runstates = "RunStates: " + ReadConstantValue(Index, "RunStates").ToString() + "\r\n";
                    string testplanStates = "TestplanStates: " + ReadConstantValue(Index, "TestplanStates").ToString() + "\r\n";
                    string resultStates = "ResultStates: " + ReadConstantValue(Index, "ResultStates").ToString() + "\r\n";
                    string mode = "Mode: " + ReadConstantValue(Index, "Mode").ToString() + "\r\n";
                    string hand0 = "HandShake00: " + ReadConstantValue(Index, "HandShake00").ToString() + "\r\n";
                    string hand1 = "HandShake01: " + ReadConstantValue(Index, "HandShake01").ToString() + "\r\n";
                    string hand2 = "HandShake02: " + ReadConstantValue(Index, "HandShake02").ToString() + "\r\n";
                    string hand3 = "HandShake03: " + ReadConstantValue(Index, "HandShake03").ToString() + "\r\n";
                    string hand4 = "HandShake04: " + ReadConstantValue(Index, "HandShake04").ToString() + "\r\n";
                    string hand5 = "HandShake05: " + ReadConstantValue(Index, "HandShake05").ToString() + "\r\n";
                    string hand6 = "HandShake06: " + ReadConstantValue(Index, "HandShake06").ToString() + "\r\n";
                    string hand7 = "HandShake07: " + ReadConstantValue(Index, "HandShake07").ToString() + "\r\n";
                    return runstates + testplanStates + resultStates + mode + hand0 + hand1 + hand2 + hand3 + hand4 + hand5 + hand6 + hand7;
                }
                else
                {
                    string runstates = "System alive: " + ReadConstantValue(Index, "System alive", false).ToString() + "\r\n";
                    string testplanStates = "Automatic mode ready: " + ReadConstantValue(Index, "Automatic mode ready", false).ToString() + "\r\n";
                    string resultStates = "System ready: " + ReadConstantValue(Index, "System ready", false).ToString() + "\r\n";
                    string mode = "Data valid: " + ReadConstantValue(Index, "Data valid").ToString() + "\r\n";
                    string checkok = "Check routine result O.K: " + ReadConstantValue(Index, "Check routine result O.K").ToString() + "\r\n";
                    string checknok = "Check routine result n.O.K: " + ReadConstantValue(Index, "Check routine result n.O.K").ToString() + "\r\n";
                    string Selftestfailed = "Self test failed: " + ReadConstantValue(Index, "Self test failed").ToString() + "\r\n";
                    string Eventlogfull = "Event log full: " + ReadConstantValue(Index, "Event log full").ToString() + "\r\n";
                    string hand0 = "OHandShake00: " + ReadConstantValue(Index, "OHandShake00", false).ToString() + "\r\n";
                    string hand1 = "OHandShake01: " + ReadConstantValue(Index, "OHandShake01", false).ToString() + "\r\n";
                    string hand2 = "OHandShake02: " + ReadConstantValue(Index, "OHandShake02", false).ToString() + "\r\n";
                    string hand3 = "OHandShake03: " + ReadConstantValue(Index, "OHandShake03", false).ToString() + "\r\n";
                    string hand4 = "OHandShake04: " + ReadConstantValue(Index, "OHandShake04", false).ToString() + "\r\n";
                    string hand5 = "OHandShake05: " + ReadConstantValue(Index, "OHandShake05", false).ToString() + "\r\n";
                    string hand6 = "OHandShake06: " + ReadConstantValue(Index, "OHandShake06", false).ToString() + "\r\n";
                    string hand7 = "OHandShake07: " + ReadConstantValue(Index, "OHandShake07", false).ToString() + "\r\n";
                    string CurrentcheckroutineID = "Current check routine ID: " + ReadConstantValue(Index, "Current check routine ID").ToString() + "\r\n";
                    string CurrentsecurityprofileID = "Current security profile ID: " + ReadConstantValue(Index, "Current security profile ID").ToString() + "\r\n";
                    string Lastcommandcode = "Last command code: " + ReadConstantValue(Index, "Last command code").ToString() + "\r\n";
                    return runstates + testplanStates + resultStates + mode + checkok + checknok + Selftestfailed + Eventlogfull + hand0 + hand1 + hand2 + hand3 + hand4 + hand5 + hand6 + hand7 + CurrentcheckroutineID + CurrentsecurityprofileID + Lastcommandcode;
                }
            }
            catch (Exception ex)
            {
                throw new AggregateException(ex.Message);
            }
        }
    }
}
